// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/vector/base_vector.h>

namespace kumo::pollux {
    // Serialization format used by the following function is documented at
    // https://facebookincubator.github.io/pollux/develop/debugging/vector-saver.html

    /// Serializes the type into binary format and writes it to the provided
    /// output stream. Used for testing.
    void saveType(const TypePtr &type, std::ostream &out);

    /// Deserializes a type serialized by 'saveType' from the provided input stream.
    /// Used for testing.
    TypePtr restoreType(std::istream &in);

    /// Serializes the vector into binary format and writes it to the provided
    /// output stream. The serialization format preserves the encoding.
    void saveVector(const BaseVector &vector, std::ostream &out);

    /// Serializes the vector into binary format and writes it to a new file in
    /// 'filePath'. The serialization preserved encoding. Exceptions will be thrown
    /// if any error occurs while writing.
    void saveVectorToFile(const BaseVector *vector, const char *filePath);

    /// Writes 'content' to a new file in 'filePath'. Exceptions will be thrown if
    /// any error occurs while writing.
    void saveStringToFile(const std::string &content, const char *filePath);

    /// Deserializes a vector serialized by 'save' from the provided input stream.
    VectorPtr restoreVector(std::istream &in, memory::MemoryPool *pool);

    /// Reads and deserializes a vector from a file stored by saveVectorToFile()
    /// method call
    VectorPtr restoreVectorFromFile(const char *filePath, memory::MemoryPool *pool);

    /// Reads a string from a file stored by saveStringToFile() method
    std::string restoreStringFromFile(const char *filePath);

    /// Serializes a SelectivityVector into binary format and writes it to the
    /// provided output stream.
    void saveSelectivityVector(const SelectivityVector &rows, std::ostream &out);

    /// Deserializes a SelectivityVector serialized by 'saveSelectivityVector' from
    /// the provided input stream.
    SelectivityVector restoreSelectivityVector(std::istream &in);

    void saveSelectivityVectorToFile(
        const SelectivityVector &rows,
        const char *filePath);

    SelectivityVector restoreSelectivityVectorFromFile(const char *filePath);
} // namespace kumo::pollux
